🐍 Activity 1 – Appending text to a file
🐍 Connect to the file in append mode
Write this line of code to create a connection to a text file
f = open("travel.txt","a")
🔎 What is f = open()
This simply connects a variable in python to an external file (a text file in the same folder)
- From this point on, all we need to do is use f when we want to do things with the file
- We can use another variable name but f is easier for a programmer to remember
(connecting to the file)
⬇️
f = open("travel.txt","a")
🔎 What is "travel.txt"
You need to write the name of the file (including .txt)
- .txt tells python this is a text file
- This is the first of two things that go inside of the brackets
(file name)
⬇️
f = open("travel.txt","a")
🔎 What does "a" do?
Python can access other files in one of three different modes
- Read (simply copying the content of the file into the python program)
- Write (replacing the content of the file with new content)
- Append (adding new content to the file - at the end)
In this lesson, we will just focus on appending to a file "a"
(append mode)
⬇️
f = open("travel.txt","a")
🐍 Writing a destination to the travel file (1)
We will now allow the user to input a destination.
This will then be written to the file
- Create a variable called place
- Assign an input to the variable
- Use the prompt text "Enter a destination you would like to travel to: "
Click to see how this is written
place = input("Enter a destination you would like to travel to: ")
🐍 Writing a destination to the travel file (2)
We will now take the user input and write it to the file
f.write(place)
🐍 Writing a destination to the travel file (3)
Let the user know that their destination has been successfully added!
Click to see how this is written
print(f"Destination: {place} Added!")
🐍 Closing the connection to the file
It is really important to always close the connection to a file once you are finished with it.
This is because it is open in a particular mode e.g. append, however you might wish to open it later in another mode e.g. read
f.close()
🐍 Run the program
- Run the program three times
- Enter a different country each time
Enter a destination you would like to travel to: Spain Destination Added!
Enter a destination you would like to travel to: France Destination Added!
Enter a destination you would like to travel to: Portugal Destination Added!
🔎 Now open the file travel.txt
Something isn't quite right!
SpainFrancePortugal
- Delete the text in the file
- Advance to the next slide to see how we fix this ➡️
🐍 Adding the new-line-character
Find this line of code
f.write(place)
Change it to this
f.write(place + "\n")
- ⬆️ This adds the new-line-character (\n) to the end of the text
- "\n" tells the computer to start a new line
- "\n" won't be displayed when you view the file
- However, it is still there!
🐍 Run and check it worked!
- Run the program three times again.
- Each time, type a travel destination
- The file should look like this!
Spain France Portugal
Don't forget to answer the questions at the bottom of the python file!
🐍 Activity 2 – Adding multiple variables to a text file
🔎 Understand The Activity
In this program you will add details of your Week A time table to a file
The goal is to have a text file that looks like this
Mon A : Lesson 1 : Computing : Mr Meehan Mon A : Lesson 2 : History : Mrs North
etc...
🐍 Creating the connection
- Create a connection to the file timetable.txt
- Make sure it is in append "a" mode
Click to see how this should look
f = open("timetable.txt","a")
🐍 Creating multiple inputs
Create variables and inputs (with suitable prompts) for the following:
- The day
- The lesson number
- The subject
- The teacher
Click to see how this should look
day = input("Enter the day of the lesson: ")
ln = input("Enter the lesson number: ")
subject = input("Enter the subject: ")
teacher = input("Enter the teacher: ")
🐍 Concatenate your inputs
- We will now join the inputs together
- This is called concatenation
- We use the + symbol to concatenate
day + " - Week A : Lesson " + ln + " : " + subject + " : " + teacher
day + " - Week A : Lesson " + ln + " : " + subject + " : " + teacher + "\n"
entry = day + " - Week A : Lesson " + ln + " : " + subject + " : " + teacher + "\n"
🐍 Write the concatenated input to the file
- Use the f.write() function to effectively write the information
Click to see how this should look
f.write(entry)
🐍 Notify the user
Let the user know that their lesson has been successfully added!
Click to see how this is written
print(f"{entry} --- added!")
🐍 Close the connection
Make sure you close the connection to the file
Click to see how this is written
f.close()
🐍 Run the program to check it works!
- Run the program to see if it works:
- Enter your lessons for Monday A!
- Open the file to see if it is successfully written to
- Answer the questions at the bottom of the code file
🐍 Activity 3 – Independent Task
🔎 Requirements of this activity
- In this program you will create your own file editor
- It will be for a topic of your choice
- The program will allow a file to be built based on a topic / interest of your choice
- I have chosen a track information file for music, but your context can be anything
Example of the type of file it will make
Track : Artist : Running Time : Year Bohemian Rhapsody : Queen : 5:55 : 1975 Heroes : David Bowie : 6:07 : 1977 Running up that hill : 4:55 : 1985
🐍 Activity 3 - Extension
- See if you can get the program to run in a big while loop
- Each time they are asked to type (1) To enter a new thing or (2) to exit
- This will allow them to keep adding new things without having to re-run the program
Click to see an outline of how this will look
while True:
print("Enter (1) to enter another track (2) to exit the program")
choice = input("➡️️ ")
if choice == "1":
[[[The lines of code for your file writing app]]]
elif choice == "2":
print("Program Exited - Goodbye 😃")
break
else:
print("Invalid choice")